home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9543 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: amaryllisp1.appsig.com!user
  2. From: larry_kearney@appsig.com (Larry Kearney)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Handling complex numbers...
  5. Date: Fri, 08 Mar 1996 07:47:22 -0700
  6. Organization: Who ever said I was organized?
  7. Distribution: world
  8. Message-ID: <larry_kearney-0803960747220001@amaryllisp1.appsig.com>
  9. References: <4hi113$2i8k@mercury.cc.uottawa.ca>
  10. NNTP-Posting-Host: amaryllisp1.appsig.com
  11.  
  12. > Dear fellow netters,
  13. > Can someone kindly explain to me how to represent complex numbers in C?
  14. > I am writing a numerical method program to calculate the area under
  15. > a curve using Simpson's Rule.  The problem that I face right now is
  16. > the representation of "i" (where i^2 = -1) in my C program.
  17. > Here's a simple example of what I mean:
  18. >   
  19. >   _b
  20. >  |           
  21. >  | exp(ix) dx 
  22. > _|             
  23. > a
  24. > How do I implement this function exp(ix)?
  25. > I'm grateful for your help.
  26. > Charles Tran
  27. > -- 
  28. > Charles
  29.  
  30. The C language is not terribly efficient when it comes to complex numbers.
  31. You can represent a complex number as a struct, i.e.,
  32.  
  33. typedef struct
  34. {
  35.    double re;  /* real part of the complex number */
  36.    double im;  /* imaginary part of the complex number */
  37. } complex;
  38.  
  39. but the big problem is that the arithmetic operators don't know what to do
  40. when presented with values that are structures. As a result, the
  41. programmer is required to implement functions that perform that standard
  42. operations and that take complex structs as arguments. For example,
  43.  
  44. void AddComplex ( complex *a, complex *b, complex *c )
  45. {
  46.    c->real = a->real + b->real;
  47.    c->imag = a->imag + b->imag;
  48. }
  49.  
  50. This makes coding an expressions such as
  51.  
  52.    x = 2.0 * ( y + z ) / d     /* all variables are complex */
  53.  
  54. an exercise is calling functions and the creation of lots of temporary
  55. complex arguments.
  56.  
  57. I'm sorry I can't offer you any more guidance about an more acceptable solution
  58. using the C language other that to suggest you surf the net looking for
  59. library packages that already implement complex arithmetic so that you
  60. don't have to write them yourself (but you better test them first).
  61. Alternate
  62. solutions would be to attempt to code your function using either Fortran, where
  63. complex arithmetic is built-in (does anybody know that anymore) or code it
  64. in C++ where you can easily create a complex class and then write function
  65. that overrides the normal arithmetic operators and trig functions when
  66. complex arithmetic is used. This approach would allow a more natural
  67. implementation of expressions.
  68.  
  69. Good luck.
  70.  
  71. -- 
  72. Larry Kearney                   |   "You want fries with that?"
  73. Applied Signal Technology       |
  74. larry_kearney@appsig.com        |
  75.